
//
// 120725 - AH - (Bergen Off. Bibliotek) Created.
//

package org.soekrat.server.servlets.account;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.japril.servlet.FetchParameterResult;
import org.japril.servlet.HttpRequestDef;
import org.soekrat.server.io.db.Account;
import org.soekrat.server.io.db.SoekratDAO;
import org.soekrat.server.io.db.SoekratDAOFactory;
import org.soekrat.server.servlets.AbstractSoekratHttpServlet;

/**
 * <p>
 *   Implements the request to get a named account. 
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class GetNamedAccountHttpServlet extends AbstractSoekratHttpServlet
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public GetNamedAccountHttpServlet(){}

  @Override
  protected void handleRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException 
  {
    HttpRequestDef def=getHttpRequestDef();
    
    if (!hasParameters(req))
    {  
      PrintWriter pw=printSimpleHtmlStart(res);
      pw.println(def.compileApiHtml());
      printSimpleHtmlEnd(pw);
    }
    else
    {
      if (!validate(res, def, req)) return;
      
      FetchParameterResult name=def.fetch(VISITOR_NAME.name(), req);
        
      SoekratDAO dao=new SoekratDAOFactory().create();
      
      Account account=dao.getAccount(name.getStringValue());
      
      if (account==null) respond(res, "not found");
      else               respond(res, account);
    }
    
  }

  @Override
  public HttpRequestDef getHttpRequestDef()
  {
    return new HttpRequestDef("/soekrat_server/get_named_account")
      .desc("Get Named Account").parameter(VISITOR_NAME);
  }
  
}
